﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(InvokeWebServiceDesignerTheme))> _
    Friend NotInheritable Class InvokeWebServiceDesigner
        Inherits ActivityDesigner
        ' Methods
        Protected Overrides Sub OnActivityChanged(ByVal e As ActivityChangedEventArgs)
            MyBase.OnActivityChanged(e)
            If (Not e.Member Is Nothing) Then
                If ((e.Member.Name = "ProxyClass") AndAlso (Not MyBase.Activity.Site Is Nothing)) Then
                    Dim activity As InvokeWebServiceActivity = [TryCast](e.Activity,InvokeWebServiceActivity)
                    PropertyDescriptorUtils.SetPropertyValue(MyBase.Activity.Site, TypeDescriptor.GetProperties(MyBase.Activity).Item("MethodName"), MyBase.Activity, String.Empty)
                    Dim service As IExtendedUIService = DirectCast(MyBase.Activity.Site.GetService(GetType(IExtendedUIService)), IExtendedUIService)
                    If (service Is Nothing) Then
                        Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IExtendedUIService).FullName }))
                    End If
                    If (activity.ProxyClass Is Nothing) Then
                        Me.url = Nothing
                    Else
                        Dim urlForProxyClass As Uri = service.GetUrlForProxyClass(activity.ProxyClass)
                        Me.url = If((Not urlForProxyClass Is Nothing), urlForProxyClass.ToString, String.Empty)
                    End If
                End If
                If (((e.Member.Name = "MethodName") OrElse (e.Member.Name = "TargetWorkflow")) AndAlso TypeOf e.Activity Is InvokeWebServiceActivity) Then
                    [TryCast](e.Activity,InvokeWebServiceActivity).ParameterBindings.Clear
                End If
                If ((e.Member.Name = "ProxyClass") OrElse (e.Member.Name = "MethodName")) Then
                    TypeDescriptor.Refresh(e.Activity)
                End If
            End If
        End Sub

        Protected Overrides Sub PreFilterProperties(ByVal properties As IDictionary)
            MyBase.PreFilterProperties(properties)
            If (properties.Item("URL") Is Nothing) Then
                properties.Item("URL") = New WebServiceUrlPropertyDescriptor(MyBase.Activity.Site, TypeDescriptor.CreateProperty(MyBase.GetType, "URL", GetType(String), New Attribute() { DesignOnlyAttribute.Yes, MergablePropertyAttribute.No }))
            End If
            If (DirectCast(MyBase.GetService(GetType(ITypeProvider)), ITypeProvider) Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
            End If
            [TryCast](MyBase.Activity,InvokeWebServiceActivity).GetParameterPropertyDescriptors(properties)
        End Sub


        ' Properties
        <SRDescription("URLDescr"), RefreshProperties(RefreshProperties.All), Editor(GetType(WebServicePickerEditor), GetType(UITypeEditor)), SRCategory("Activity")> _
        Public Property URL As String
            Get
                If (Me.url Is Nothing) Then
                    Dim activity As InvokeWebServiceActivity = [TryCast](MyBase.Activity,InvokeWebServiceActivity)
                    Dim service As IExtendedUIService = DirectCast(MyBase.Activity.Site.GetService(GetType(IExtendedUIService)), IExtendedUIService)
                    If ((Not service Is Nothing) AndAlso (Not activity.ProxyClass Is Nothing)) Then
                        Dim urlForProxyClass As Uri = service.GetUrlForProxyClass(activity.ProxyClass)
                        Me.url = If((Not urlForProxyClass Is Nothing), urlForProxyClass.ToString, String.Empty)
                    End If
                End If
                Return Me.url
            End Get
            Set(ByVal value As String)
                If (Me.url <> value) Then
                    Me.url = value
                    Dim service As IExtendedUIService = DirectCast(MyBase.Activity.Site.GetService(GetType(IExtendedUIService)), IExtendedUIService)
                    If (service Is Nothing) Then
                        Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(IExtendedUIService).FullName }))
                    End If
                    Dim transaction As DesignerTransaction = Nothing
                    Dim host As IDesignerHost = [TryCast](MyBase.Activity.Site.GetService(GetType(IDesignerHost)),IDesignerHost)
                    If (Not host Is Nothing) Then
                        transaction = host.CreateTransaction(SR.GetString("ChangingVariable"))
                    End If
                    Try 
                        PropertyDescriptorUtils.SetPropertyValue(MyBase.Activity.Site, TypeDescriptor.GetProperties(MyBase.Activity).Item("ProxyClass"), MyBase.Activity, If(String.IsNullOrEmpty(Me.url), Nothing, service.GetProxyClassForUrl(New Uri(Me.url))))
                        If (Not transaction Is Nothing) Then
                            transaction.Commit
                        End If
                    Finally
                        If (Not transaction Is Nothing) Then
                            DirectCast(transaction, IDisposable).Dispose
                        End If
                    End Try
                End If
            End Set
        End Property


        ' Fields
        Private url As String
    End Class
End Namespace

